package com.example.juyuanapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.juyuanapp.bean.LoginBean;
import com.example.juyuanapp.bean.RegisterBean;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends AppCompatActivity {
    EditText number,password;
    Button btn_register,btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        number=findViewById(R.id.ed_num);
        password=findViewById(R.id.et_pwd);
        btn = findViewById(R.id.btn_login);
        btn_register = findViewById(R.id.btn_register);
        //注册控件点击事件
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,
                        RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    RequestQueue queue= Volley.newRequestQueue(LoginActivity.this);//请求队列
                    String url="http://192.168.43.251:8080/user/login";
                    Map<String,String> map=new HashMap<>();
                    map.put("telephone",number.getText().toString());
                    map.put("password",password.getText().toString());
                    JSONObject jsonObject=new JSONObject(map);
                    //创建新的请求
                    JsonObjectRequest request=new JsonObjectRequest(Request.Method.POST, url,
                            jsonObject,
                            new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject jsonObject) {
                                    //解析后台代码
                                    Gson gson=new Gson();
                                    LoginBean loginBean = gson.fromJson(jsonObject.toString(), LoginBean.class);
                                    if(loginBean.isSuccess()){
                                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                        Intent intent=new Intent(LoginActivity.this,BookActivity.class);
                                        startActivity(intent);
                                    }else {
                                        Toast.makeText(LoginActivity.this, "用户不存在", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            },
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError volleyError) {
                                    Toast.makeText(LoginActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                                }
                            });
                    queue.add(request);  //加入请求队列
            }
        });

    }
}